<?php
/**
 * @author Pham Van Diep
 * @copyright 2014
 * @description: save edit contract
 */
// not login
// not perm add
if(!isset($AppUI->id) ||
    ! isset($_SESSION['personal']) ||
    ($_SESSION['personal']&1 << 2) == 0 //||
    //!isset($_POST['personalid'])

) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
$arrErr = array();
// data input
//var_dump($_POST); die();
$intContractId = isset($_POST['contractid']) ? intval($_POST['contractid']): -1;
if($intContractId == -1 ) {
    array_push($arrErr,$arrLocale['no_contract_id']);
}
$intType     = (int)CI_Function::stripTag(trim($this->input->post('type',-1)));
/*if ($intType == -1) {
    array_push($arrErr,$arrLocale['no_type_selected']);
}*/
$strNational = CI_Function::stripTag(trim($this->input->post('national','')));
/*if(empty($strNational)) {
    array_push($arrErr,$arrLocale['no_national_selected']);
}*/
$strLeague = (int)CI_Function::stripTag(trim($this->input->post('league',-1)));
/*if(empty($strLeague)) {
    array_push($arrErr,$arrLocale['no_league_selected']);
}*/
$intClubId = (int)CI_Function::stripTag(trim($this->input->post('clubid', -1)));
/*if($intClubId == -1) {
    array_push($arrErr,$arrLocale['no_club_selected']);
}*/
$intPersonalId = isset($_POST['personalid']) ? intval($_POST['personalid']): -1;
if($intPersonalId == -1 ) {
    array_push($arrErr,$arrLocale['peronal_id_invalid']);
}
$dblFee = CI_Function::stripTag(trim($this->input->post('fee', -1)));
if($dblFee <= 0) {
    array_push($arrErr,$arrLocale['fee_invalid']);
}
$dSigned = CI_Function::stripTag(trim($this->input->post('signed',null)));
if(empty($dSigned) || !CI_Validate::isDate($dSigned)) {
    array_push($arrErr,$arrLocale['signed_invalid']);
}
$dBegin = CI_Function::stripTag(trim($this->input->post('begin',null)));
if(empty($dBegin)  || !CI_Validate::isDate($dBegin)) {
    array_push($arrErr,$arrLocale['begin_invalid']);
}
$dEnd = CI_Function::stripTag(trim($this->input->post('end',null)));
if(empty($dEnd) || !CI_Validate::isDate($dEnd) || strtotime($dEnd) < strtotime($dBegin)) {
    array_push($arrErr,$arrLocale['end_invalid']);
}
$dExpiry = CI_Function::stripTag(trim($this->input->post('expiry',null)));
if(empty($dExpiry) || !CI_Validate::isDate($dExpiry)) {
    array_push($arrErr,$arrLocale['expiry_invalid']);
}
$strDes             = CI_Function::stripTag(trim($this->input->post('description',null)));
$intLen = strlen($strDes);
if ($intLen < VL_CONTENT_MINLENGTH ) {
    array_push($arrErr, $arrLocale['description_minlength']);
}
$strFee = CI_Function::stripTag(trim($this->input->post('fee',null)));
$intExt = (int)$this->input->post('ext',0);
$intParentId = (int)$this->input->post('parentid',0);
$intSalaryInWeek = (int)$this->input->post('salary_in_week',0); 
$strSalaryUnit = CI_Function::stripTag(trim($this->input->post('salary_unit','')));
if( count($arrErr) <= 0) {//allow save      
	if(isset($_SESSION['CONTRACT_INPUT']) ) unset($_SESSION['CONTRACT_INPUT']);
	if(isset($_SESSION['CONTRACT_ERROR']) ) unset($_SESSION['CONTRACT_ERROR']);                  
    $intError = $this->contract->add($AppUI->id,
									$intContractId,
									$intPersonalId,
									$intClubId,
									$strFee,
									CI_Function::convertStrToDate($dSigned),
									CI_Function::convertStrToDate($dBegin),
									CI_Function::convertStrToDate($dEnd),
									CI_Function::convertStrToDate($dExpiry),
									$strDes,
									$intExt,
									$intParentId,
									$intSalaryInWeek,
									$strSalaryUnit);   																											 
    if($intError == 0) CI_Function::setMsg($arrMessage['success']);
    else if($intError == -1)array_push($arrErr,$arrLocale['sys_add_err']);                        
    else array_push($arrErr,$arrError['problem_save_db_error']);            
}
// occur error
if ( count($arrErr) > 0) {       
    $_SESSION['CONTRACT_INPUT'] = $this->input->post();
    $_SESSION['CONTRACT_ERROR'] = $arrErr;    
    redirect(HOST_BACKEND . 'contract/edit?contractid='.$intContractId);
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);

